package store.lunangangster.nodeflow.autoconfigure;

import com.ql.util.express.IExpressContext;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import store.lunangangster.nodeflow.config.NodeFlowConfig;
import store.lunangangster.nodeflow.context.FlowContext;
import store.lunangangster.nodeflow.context.holder.FlowContextHolder;
import store.lunangangster.nodeflow.parser.el.ELContextHolder;
import store.lunangangster.nodeflow.rt.NodeFlowRuntime;

/**
 * NodeFlow系统初始化器，用于SpringBoot初始化bean之后NodeFlow的自动初始化工作
 *
 * @author cwk
 * @version 1.0
 */
public class NodeFlowInitializer implements InitializingBean {
	@Autowired
	FlowContext context;
	@Autowired
	IExpressContext<String, Object> elContext;

	private final NodeFlowConfig nodeFlowConfig;

	public NodeFlowInitializer(NodeFlowConfig nodeFlowConfig) {
		this.nodeFlowConfig = nodeFlowConfig;
	}

	@Override
	public void afterPropertiesSet() {
		if (nodeFlowConfig.getParseOnStart()) {
			// 0. 将FlowContext设置到FlowContextHolder
			FlowContextHolder.setContext(context);
			// 1. 将ELContext设置到ELContextHolder中
			ELContextHolder.setELContext(elContext);
			// 2. 初始化NodeFlowRuntime
			NodeFlowRuntime.init();
		}
	}
}
